package co.yixiang.yshop.module.controller.admin.webprint;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.controller.admin.webprint.vo.*;
import co.yixiang.yshop.module.dal.dataobject.webprint.WebPrintDO;
import co.yixiang.yshop.module.convert.webprint.WebPrintConvert;
import co.yixiang.yshop.module.service.webprint.WebPrintService;

@Tag(name = "管理后台 - 云打印机")
@RestController
@RequestMapping("/store/web-print")
@Validated
public class WebPrintController {

    @Resource
    private WebPrintService webPrintService;

    @PostMapping("/create")
    @Operation(summary = "创建云打印机")
    @PreAuthorize("@ss.hasPermission('store:web-print:create')")
    public CommonResult<Long> createWebPrint(@Valid @RequestBody WebPrintCreateReqVO createReqVO) {
        return success(webPrintService.createWebPrint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新云打印机")
    @PreAuthorize("@ss.hasPermission('store:web-print:update')")
    public CommonResult<Boolean> updateWebPrint(@Valid @RequestBody WebPrintUpdateReqVO updateReqVO) {
        webPrintService.updateWebPrint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除云打印机")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('store:web-print:delete')")
    public CommonResult<Boolean> deleteWebPrint(@RequestParam("id") Long id) {
        webPrintService.deleteWebPrint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得云打印机")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('store:web-print:query')")
    public CommonResult<WebPrintRespVO> getWebPrint(@RequestParam("id") Long id) {
        WebPrintDO webPrint = webPrintService.getWebPrint(id);
        return success(WebPrintConvert.INSTANCE.convert(webPrint));
    }

    @GetMapping("/list")
    @Operation(summary = "获得云打印机列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('store:web-print:query')")
    public CommonResult<List<WebPrintRespVO>> getWebPrintList(@RequestParam("ids") Collection<Long> ids) {
        List<WebPrintDO> list = webPrintService.getWebPrintList(ids);
        return success(WebPrintConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得云打印机分页")
    @PreAuthorize("@ss.hasPermission('store:web-print:query')")
    public CommonResult<PageResult<WebPrintRespVO>> getWebPrintPage(@Valid WebPrintPageReqVO pageVO) {
        PageResult<WebPrintDO> pageResult = webPrintService.getWebPrintPage(pageVO);
        return success(WebPrintConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出云打印机 Excel")
    @PreAuthorize("@ss.hasPermission('store:web-print:export')")
    @OperateLog(type = EXPORT)
    public void exportWebPrintExcel(@Valid WebPrintExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WebPrintDO> list = webPrintService.getWebPrintList(exportReqVO);
        // 导出 Excel
        List<WebPrintExcelVO> datas = WebPrintConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "云打印机.xls", "数据", WebPrintExcelVO.class, datas);
    }

}
